/**
 移动页面Controller
 @class logs
 @author :laiwp
 @date:  :2016-07-01
 @version : 0.1.0
 @constructor : 0.1.0
 */
var config = require('../settings');
var log = require('../logger.js');
var tools = require('../tools.js');
var https = require('https');
var async = require('async');
var md5 = require('md5');

var moment = require('moment');
var superagent = require('superagent');
var settings = require('../settings');

var dbDao = require('../models/dbBase.js');

module.exports = {
    mapping: {
        _sport_order_pay: {
            'url': '/sport/order/pay',
            'method': 'get',
            'description': '订单支付',
            'auth': false
        },
        _sport_getSignature: {
            'url': '/sport/getSignature',
            'method': 'get',
            'description': '查询可用场地',
            'auth': false
        },
    },

    //订单支付
    _sport_order_pay: function (req, res) {
        console.log("这个页面点击后微信收款");
        var orderNo = req.query.orderNo || '';
        var wxopenid = req.query.wxopenid || '';
        if((!orderNo)&&(wxopenid)){
            res.send('订单号或微信openid无效...');
        }else{
            var sql = "select * from orders where id='" + orderNo + "'";
            dbDao.find( sql, function (err, result) {
                if (result.length > 0) {
                    var sql2 = "select * from Gymnasium where id='" + result[0].gymId + "'";
                    dbDao.find(sql2, function (err, result2) {
                        //})opo4Fws5LKKN9UFG2jvfz4N-jz1k
                        superagent.get('http://go.sportscv.cn:8804/webpay')
                            .query('openid='+wxopenid)
                            .query('out_trade_no='+orderNo)
                            .end(function(err, result){
                                if(err){
                                    console.log("error:", err);
                                    res.send({result:'error'});
                                }else{
                                    if(result&&result.body){
                                        console.log(result.body);
                                        res.send(result.body);
                                    }else{
                                        res.send({})
                                    }
                                }
                            });
                    });
                } else {
                    log.logger.info('查不到订单err：' + err + 'orderNo=' + orderNo);
                    res.send('订单号无效...');
                }
            });
        }
    },
    _sport_getSignature: function (req,res){

        console.log("_sport_getSignature url:");
        var qUrl = req.headers.referer;

        console.log("---params:", qUrl);
        superagent.get('http://go.sportscv.cn:8804/getSignature')
            .query('qurl='+qUrl)
            .end(function(err, result){
                if(err){
                    console.log("error:", err);
                    res.send({result:'error'});
                }else{
                    if(result&&result.body){
                        console.log(result.body);
                        res.send(result.body);
                    }else{
                        res.send({})
                    }
                }
            });

    },

}
